package com.kelvin.paopao.module.login.register.resetpwd;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kelvin.kelvinlib.utils.CommonUtil;
import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.injector.components.DaggerResetPwdComponent;
import com.kelvin.paopao.injector.modules.ResetPwdModule;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.login.register.registerStep2.IRegisterStep2View;
import com.kelvin.paopao.widgets.CustomToolbar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/5.
 */

public class ResetPwdActivity extends BaseActivity<ResetPwdPresenter> implements IRegisterStep2View {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_yzm)
    EditText etYzm;
    @BindView(R.id.tv_retry)
    TextView tvRetry;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_password_confirm)
    EditText etPasswordConfirm;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_reset_pwd;
    }

    @Override
    protected void initInjector() {
        DaggerResetPwdComponent.builder().resetPwdModule(new ResetPwdModule(this))
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        customToolbar.showBackIcon(this);
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick({R.id.tv_retry, R.id.tv_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_retry:
                String phone = etPhone.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast(this, "请输入要找回的账号~~");
                    return;
                }
                if (!CommonUtil.isPhoneNo(phone)) {
                    ToastUtil.showToast(this, "手机格式不正确~~");
                    return;
                }
                mPresenter.getValiadate(phone);
                break;
            case R.id.tv_confirm:
                String pwd = etPassword.getText().toString();
                String pwdConfirm = etPasswordConfirm.getText().toString();
                String code = etYzm.getText().toString();
                String mobile = etPhone.getText().toString();
                if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwdConfirm) || TextUtils.isEmpty(code)
                        || TextUtils.isEmpty(mobile)) {
                    ToastUtil.showToast(this, "请填写完整~~");
                    return;
                }
                if (!pwd.equals(pwdConfirm)) {
                    ToastUtil.showToast(this, "两次输入密码不一致~~");
                    return;
                }
                mPresenter.resetPwd(code, pwd,mobile);
                break;
        }
    }

    @Override
    public void updateTextView(int count) {
        tvRetry.setText(count + "秒后重试");

        if (count == 0) {
            tvRetry.setEnabled(true);
            tvRetry.setText("获取验证码");
        } else {
            tvRetry.setEnabled(false);
        }
    }

    @Override
    public void enableRetry(boolean isEnable) {
        tvRetry.setEnabled(isEnable);
    }

    public static void launch(Context context) {
        Intent intent = new Intent(context, ResetPwdActivity.class);
        context.startActivity(intent);
    }
}
